TextInputStream Class

In order to read text from a file, you need to create a TextInputStream object. TextInputStreams have methods that allow to read from a file, check to see if you are at the end of the file, and close the file when you are done reading from it.

Events

None

Properties

Encoding

LastErrorCode

PositionB


Methods

Close

ReadAll

ReadLine


More information available in parent classes: Object

They are created by calling the OpenAsTextFile method of a FolderItem object. If you are working with a file with an encoding that is not UTF-8, you should set the value of the Encoding property.


Notes

The TextInputStream class implements the Readable class interface. If you implement this interface, you must provide the methods with the parameters as specified by this class interface.

For more information about class interfaces and how to implement them, see the section "Class Interfaces" in the User's Guide.

When you read a text file that is from another operating system or in another language (or a mixture of languages) you may need to specify the text encoding that was used when the file was written. If you know the encoding, use the Encodings object to get the encoding and use it to set the value of the Encoding property of the TextInputStream object. Here is an example that reads a text file that uses the MacRoman encoding:

Dim f As FolderItem
Dim t As TextInputStream
f= GetOpenFolderItem("text") //file type defined in File Type Sets Editor
If f <> Nil then
 t=f.OpenAsTextFile
 t.Encoding= Encodings.MacRoman //specify encoding of input stream
 EditField1.text=t.ReadAll
 t.Close
End if

To specify the encoding, you can instead use optional parameter of the ReadAll method:

EditField1.Text=t.ReadAll( Encodings.MacRoman)

instead of

t.Encoding= Encodings.MacRoman


Examples

This example reads the rows and columns of data from a tab-delimited text file into a ListBox:

Dim f As FolderItem
Dim textInput As TextInputStream
Dim rowFromFile,oneCell As String
Dim i As Integer

f= GetOpenFolderItem("text/plain")  //defined as a FileType

If f <> Nil Then
 textInput = f.OpenAsTextFile
 textInput.Encoding= Encodings.MacRoman //strings are MacRoman
 Do
  rowFromFile=textInput.ReadLine
  If ListBox1.ColumnCount < CountFields(rowFromFile, Chr(9)) Then
   ListBox1.ColumnCount= CountFields(rowFromFile, Chr(9))
  End If
 ListBox1.AddRow NthField(rowFromFile, Chr(9),1)
  For i=1 to CountFields(rowFromFile, Chr(9))
   oneCell= NthField(rowFromFile, Chr(9),i)
   ListBox1.Cell(ListBox1.ListCount-1,i-1)=oneCell
  Next
 Loop Until textInput.EOF
 textInput.Close
End If

See Also

ConvertEncoding, DefineEncoding, Encoding functions; BinaryStream, TextEncoding, TextOutputStream classes; Encodings object; Readable class interface.